<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    //后台帖子板块管理
    //添加
    public function getAdd()
    {
    	return view('Admin.posttype.add');
    }

    //后台帖子模块查看删除
    public function getIndex(Request $request)
    {

        //判断是否有判断
        if($request -> input('keywords')){
            $post = DB::table('posttype')
                ->where('name','like','%'.$request->input('keywords').'%')
                ->paginate(10);
        }else{
            //获取数据库的数据
            $post = DB::table('posttype')->paginate(10);
        }
        $count = 1;
        //获取所有的数据
        $data = $request -> all();
    	return view('Admin.posttype.index',['post'=>$post,'count' => $count,'data' => $data]);
    }

    //后台帖子模块修改
    public function getEdit($id)
    {
    	//获取值
    	$update = DB::table('posttype')->where('id',$id)->first();
    	//
    	return view('Admin.posttype.edit',['update'=>$update]);
    }

    //执行修改
    public function postDoupdate(Request $request)
    {
    	$data = $request->only(['name','content']);
    	//dd($data);
    	$id = $request->input('id');
  
         //执行数据的插入
        $res = DB::table('posttype')->where('id',$id)->update($data);
        if($res){
        return redirect('/admin/posttype/index')->with('success','修改成功');
    	}else{
    		return back()->with('error','修改失败');
    	}

    }

    //添加
    public function postInsert(Request $request)
    {
    	//dd($request->all());

			//处理数据添加到数据库
        $data = $request->only(['name','content']);
        //判断模块是否重复
        //$ress = DB::table('posttype')->select($request('name'));
       
    	$ress = DB::table('posttype')->where('name',$request->input('name'))->first();
    	//dd($user); 
    	if(empty($ress)){
    		
         //执行数据的插入
        $res = DB::table('posttype')->insert($data);
        return redirect('/admin/posttype/index')->with('success','添加成功成功');
    	}else{
    		return back()->with('error','此板块已存在');
    	}

        
    }
    //删除
    	public function getDelete($id)
    	{
    		
    		//dd($id);
    		$res = DB::table('posttype')->where('id',$id)->delete();
    		if($res){
        return redirect('/admin/posttype/index')->with('success','删除成功');
    	}else{
    		return back()->with('error','删除失败');
    	}
    	
    	}
    
  
}
